package com.github.shiqiyue.app.api.sapi.controller.dbms;


import com.github.shiqiyue.app.api.sapi.common.SApiBaseController;
import com.github.shiqiyue.app.api.sapi.controller.dbms.dto.UserSqlCollectListDTO;
import com.github.shiqiyue.app.api.sapi.controller.dbms.req.UserSqlCollectAddReq;
import com.github.shiqiyue.app.api.sapi.controller.dbms.req.UserSqlCollectEditReq;
import com.github.shiqiyue.app.common.result.Result;
import com.github.shiqiyue.app.modules.dbms.entity.UserSqlCollect;
import com.github.shiqiyue.app.modules.dbms.service.IUserSqlCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户收藏的SQL 前端控制器
 * </p>
 *
 * @author wenyao.wu
 * @since 2019-08-06
 */
@RestController
@RequestMapping("/sapi/dbms/user-sql-collect")
public class ApiUserSqlCollectController extends SApiBaseController {

    @Autowired
    private IUserSqlCollectService userSqlCollectService;

    /***
     * 获取用户收藏的SQL
     * @return
     */
    @PostMapping("list")
    public Result list(Long databaseId) {
        if (databaseId == null) {
            return Result.fail("参数错误");
        }
        Long userId = getCurrentUser().getId();
        List<UserSqlCollect> collectList = userSqlCollectService.listByUserIdAndDatabaseId(userId, databaseId);
        UserSqlCollectListDTO userSqlCollectListDTO = new UserSqlCollectListDTO();
        userSqlCollectListDTO.setRecords(collectList.stream().map(UserSqlCollectListDTO.Item::newInstance).collect(Collectors.toList()));
        return Result.success(userSqlCollectListDTO);
    }

    /***
     * 添加用户SQL收藏记录
     * @param userSqlCollectAddReq
     * @param bindingResult
     * @return
     */
    @PostMapping("add")
    public Result add(@Valid UserSqlCollectAddReq userSqlCollectAddReq, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        Long userId = getCurrentUser().getId();
        UserSqlCollect userSqlCollect = new UserSqlCollect();
        userSqlCollect.setName(userSqlCollectAddReq.getName());
        userSqlCollect.setData(userSqlCollectAddReq.getSql());
        userSqlCollect.setUserId(userId);
        userSqlCollect.setDatabaseId(userSqlCollectAddReq.getDatabaseId());
        userSqlCollectService.save(userSqlCollect);
        return Result.success();
    }

    /***
     * 删除
     * @param id
     * @return
     */
    @PostMapping("del")
    public Result del(Long id) {
        if (id == null) {
            return Result.fail("参数错误");
        }
        Long userId = getCurrentUser().getId();
        userSqlCollectService.deleteByUserIdAndId(userId, id);
        return Result.success();
    }

    /***
     * 修改
     * @param userSqlCollectEditReq
     * @param bindingResult
     * @return
     */
    @PostMapping("edit")
    public Result edit(@Valid UserSqlCollectEditReq userSqlCollectEditReq, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        Long userId = getCurrentUser().getId();
        UserSqlCollect userSqlCollect = userSqlCollectService.getById(userSqlCollectEditReq.getId());
        if (userSqlCollect == null) {
            return Result.fail("记录不存在");
        }
        if (!userSqlCollect.getUserId().equals(userId)) {
            return Result.fail("记录不存在");
        }
        UserSqlCollect updateParam = new UserSqlCollect();
        updateParam.setId(userSqlCollectEditReq.getId());
        updateParam.setData(userSqlCollectEditReq.getSql());
        userSqlCollectService.updateById(updateParam);
        return Result.success();
    }

}
